package com.ctg.itrdc.sysmgr.portal.control;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.pasp.core.PageInfo;
import com.ctg.itrdc.pasp.security.SecUser;
import com.ctg.itrdc.pasp.security.utils.WebUtils;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemPostDTO;
import com.ctg.itrdc.sysmgr.portal.api.service.ISystemPostService;
import com.ctg.itrdc.sysmgr.portal.common.entity.CommonResult;
import com.ctg.itrdc.sysmgr.portal.utils.DTOHelper;

@Controller
@RequestMapping("/system_post")
public class SystemPostController extends SysmgrCURDController<Long, SystemPostDTO> {
    
	@Resource
	private ISystemPostService systemPostService;
	
	@RequestMapping("/insert")
	@ResponseBody
	public CommonResult insert(@Valid SystemPostDTO dto ,BindingResult result) throws Exception {
		DTOHelper.valid(result);
		DTOHelper.updateDTO(dto);
		long i = systemPostService.insert(dto);
		return new CommonResult(HttpStatus.OK.toString(), "新增数据成功",i);
	}
	
	@RequestMapping("/update")
	@ResponseBody
	@Override
	public CommonResult updateByPrimaryKey(SystemPostDTO dto, BindingResult result) throws Exception {
		DTOHelper.valid(result);
		DTOHelper.updateDTO(dto);
		int i = systemPostService.updateByPrimaryKey(dto);
		return new CommonResult(HttpStatus.OK.toString(), "更新数据成功",i);
	}



	/**
	 * @api {get} /system_post/page/orgId 岗位分页查询
	 * @apiGroup system_post
	 * @apiName queryPageInfoByOrgId
	 * @apiDescription 获取岗位分页。
	 * @apiParam {Long} orgId 选传 管理id, 有则查询管理组织下的岗位
	 * @apiParam {Long} rzOrgId 选传 任职id,有则查询其他部门授予给该组织的使用岗位。
	 * 		使用权限只能看，不能作删改、跨部门授权、关键角色、私有权限等操作。
	 * @apiParam {Integer} currentPage 默认为 0 
	 * @apiParam {Integer} perPage 默认 10
	 * @apiVersion 0.0.1
	 */
	@RequestMapping("/page/orgId")
	@ResponseBody
	public CommonResult queryPageInfoByOrgId(SystemPostDTO dto,
			@RequestParam(defaultValue = "0") Integer currentPage,
			@RequestParam(defaultValue = "10") Integer perPage)
			throws Exception {
		PageInfo<SystemPostDTO> pageInfo = systemPostService.queryPageInfo(dto,dto.getSysUserId(),
				currentPage, perPage);
		return new CommonResult(HttpStatus.OK.toString(), "分页查询成功", pageInfo);
	}
	
	
	/**
	 * @api {get} /system_post/query/sysUserId 岗位列表查询
	 * @apiGroup system_post
	 * @apiName selectBySysUserId
	 * @apiDescription 根据系统用户id，获取系统用户对应岗位列表
	 * @apiParam {Long} sysUserId 必传 系统用户id
	 * @apiVersion 0.0.1
	 */
	@RequestMapping("/query/sysUserId")
	@ResponseBody
	public CommonResult selectBySysUserId(
			@RequestParam(required = false ) Long sysUserId){
		if(sysUserId==null){
			SecUser ctgUser = WebUtils.getCtgUser();
			if(ctgUser!=null && ctgUser.getSysUserId()!=null){
				sysUserId = ctgUser.getSysUserId();
			}
		}
		List<SystemPostDTO> resultList = systemPostService.selectByUId(sysUserId);
		return new CommonResult(HttpStatus.OK.toString(), "岗位列表查询成功", resultList);
	}
}
